﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using xyjc;

namespace app
{
    public partial class ModifyTemplateForm : Form
    {
        private Form1 parentForm { get; set; }
        private ModifyWordTemplate modifier { get; set; }
        private Dictionary<string, TextBox> keysPanelTextBoxes { get; set; }

        public ModifyTemplateForm(Form1 parent)
        {
            InitializeComponent();

            parentForm = parent;
            
            ChangeTemplate(parentForm.xyjcApp.Config.TemplateFile);
            RefreshTemplateList();
        }

        private void RefreshTemplateList()
        {
            string[] files = Directory.GetFiles(parentForm.xyjcApp.Config.TemplatePath, "*.doc*", SearchOption.AllDirectories);
            selectTemplateFileComboBox.Items.Clear();
            foreach (string file in files)
            {
                selectTemplateFileComboBox.Items.Add(file);
            }
        }

        private void RefreshKeysPanel()
        {
            Dictionary<string, TextBox> temp = new Dictionary<string, TextBox>();
            keysPanel.Controls.Clear();
            int top = 0;
            foreach(var k in modifier.StaticKeys)
            {
                string type = "textrange";
                if(k.Value.ContainsKey("type"))
                {
                    type = k.Value["type"];
                }

                switch (type)
                {
                    case "picture":
                        {
                            TextBox textBox = new TextBox();
                            textBox.Text = k.Value["defaultValue"];
                            textBox.Anchor = ((AnchorStyles)((AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top)));
                            textBox.Location = new System.Drawing.Point(110, 4 + top);
                            textBox.Size = new System.Drawing.Size(200, 21);

                            KeyButton button = new KeyButton(textBox);
                            button.Text = k.Value["label"];
                            button.Location = new System.Drawing.Point(8, 4 + top);
                            button.Size = new System.Drawing.Size(100, 21);

                            keysPanel.Controls.Add(button);
                            keysPanel.Controls.Add(textBox);
                            temp.Add(k.Key, textBox);
                            top += 25;
                            break;
                        }
                    case "textrange":
                        {
                            TextBox textBox = new TextBox();
                            textBox.Text = k.Value["defaultValue"];
                            textBox.Anchor = ((AnchorStyles)((AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top)));
                            textBox.Location = new System.Drawing.Point(110, 4 + top);
                            textBox.Size = new System.Drawing.Size(200, 21);

                            Label label = new Label();
                            label.Text = k.Value["label"];
                            label.TextAlign = ContentAlignment.MiddleCenter;
                            label.Location = new System.Drawing.Point(8, 8 + top);
                            label.Size = new System.Drawing.Size(100, 21);

                            keysPanel.Controls.Add(label);
                            keysPanel.Controls.Add(textBox);
                            temp.Add(k.Key, textBox);
                            top += 25;
                            break;
                        }
                    default:
                        {
                            TextBox textBox = new TextBox();
                            textBox.Text = k.Value["defaultValue"];
                            textBox.Multiline = true;
                            textBox.Anchor = ((AnchorStyles)((AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top)));
                            textBox.Location = new System.Drawing.Point(110, 4 + top);
                            textBox.Size = new System.Drawing.Size(200, 71);

                            Label label = new Label();
                            label.Text = k.Value["label"];
                            label.TextAlign = ContentAlignment.MiddleCenter;
                            label.Location = new System.Drawing.Point(8, 8 + top);
                            label.Size = new System.Drawing.Size(100, 21);

                            keysPanel.Controls.Add(label);
                            keysPanel.Controls.Add(textBox);
                            temp.Add(k.Key, textBox);
                            top += 75;
                            break;
                        }
                }
            }
            keysPanelTextBoxes = temp;
        }

        private void ChangeTemplate(string fileName)
        {
            modifier = parentForm.xyjcApp.GetModifyInstance(fileName);
            newTemplateNameTextBox.Text = fileName;
            selectTemplateFileComboBox.Text = fileName;
            RefreshKeysPanel();
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            if(newTemplateNameTextBox.Text.IndexOf(".doc") != -1)
            {
                foreach(var k in keysPanelTextBoxes)
                {
                    modifier.StaticKeys[k.Key]["defaultValue"] = k.Value.Text;
                }
                try
                {
                    modifier.SaveTemplate(newTemplateNameTextBox.Text);
                    MessageBox.Show("保存成功！");
                    RefreshTemplateList();
                    ChangeTemplate(newTemplateNameTextBox.Text);
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.Message);
                }
            }
            else
            {
                MessageBox.Show("必须保存为.doc或.docx文件");
            }
        }

        private void selectTemplateFileComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            ChangeTemplate(selectTemplateFileComboBox.Text);
        }

        public void ShowIt()
        {
            parentForm.EnabledEditTemplateButton(false);
            Show();
        }

        public void CloseIt()
        {
            parentForm.EnabledEditTemplateButton(true);
            Close();
        }

        private void ModifyTemplateForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            parentForm.EnabledEditTemplateButton(true);
        }
    }

    class KeyButton: Button
    {
        private TextBox textBox { get; set; }

        public KeyButton(TextBox theTextBox)
        {
            textBox = theTextBox;
        }

        protected override void OnClick(EventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Multiselect = false;
            fileDialog.Title = "请选择图片文件";
            fileDialog.Filter = "图片|*.jpg;*.png"; //设置要选择的文件的类型
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                textBox.Text = fileDialog.FileName;
            }
        }
    }
}
